var express = require('express');
var router = express.Router();
const userService = require('../service/userService');

/* 获取优惠券数据 */
router.get('/getuser', async function (req, res) {
   let { current, pageSize,type,value } = req.query;
   let searchParams = {};
   if (type) {
      if (type == "age") {
         searchParams[type] = value;
      } else {
         searchParams[type] = { $regex: value };
      }
   }
   console.log(req.query);
   let data = await userService.find({ current: current || 1, pageSize: pageSize || 5 },searchParams);
   res.send(data);
});


// id查找
router.get('/findbyid', async function (req, res) {
   let { id } = req.query;
   let data = await userService.findById(id);
   res.send(data);
});


// 添加
router.post('/insert', async function (req, res) {
   let { name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare} = req.body;
   console.log(name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare);
   let data = await userService.insert({ name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare });
   res.send(data);
});


//删除
router.delete('/remove', async function (req, res) {
   let { id } = req.body;
   console.log(id);
   let data = await userService.delete(id);
   res.send(data);
});

// 修改
router.put('/update', async function (req, res) {
   let { _id, name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare } = req.body;
   console.log(_id, name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare)
   let data = await userService.update({ _id, name, pwd,codeImg,phone,volume,coin,balance,card,grade,tast,userAdr,orderId,spare });
   res.send(data);
});


module.exports = router;
